import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes: [
		{
			path: '',
			name: 'index',
			redirect: "/Home",
		},
		{
			path: '/about',
			name: 'about',
			component: () => import('../Views/About.vue')
		},
		{
			path: '/login',
			name: 'login',
			component: () => import('../Views/Login.vue')
		},
		{
			path: '/home',
			name: 'home',
			component: () => import('../Views/Home.vue'),
			redirect: "/welcome",
			children: [
				{
					path: '/welcome',
					name: 'welcome',
					component: () => import('../Views/Welcome.vue')
				},
				{
					path: '/Role',
					name: 'Role',
					component: () => import('../Views/Role/RoleList.vue')
				},

				{
					path: '/User',
					name: 'User',
					component: () => import('../Views/User/UserList.vue')
				},
				{
					path: '/Menu',
					name: 'Menu',
					component: () => import('../Views/Menu/MenuList.vue')
				},
				{
					path: '/Dict',
					name: 'Dict',
					component: () => import('../Views/Dict/DictList.vue')
				},
				{
					path: '/Orders',
					name: 'Orders',
					component: () => import('../Views/Orders/OrdersList.vue')
				},
				{
					path: '/Produ',
					name: 'Produ',
					component: () => import('../Views/Produ/ProduList.vue')
				},
				{
					path: '/DictType',
					name: 'DictType',
					component: () => import('@/Views/DictType/DictTypeList.vue')
				}
				,
				{
					path: '/Business',
					name: 'Business',
					component: () => import('@/Views/Business/BusinessList.vue')
				}
				,
				{
					path: '/Client',
					name: 'Client',
					component: () => import('../Views/Client/ClientList.vue')
				},
				{
					path:'/Project',
					name:'Project',
					component:()=>import('@/Views/Project/ProjectList.vue')
				}
				,
				{
					path:'/Task',
					name:'Task',
					component:()=>import('@/Views/Task/TaskList.vue')
				}
				// ,
				// {
				// 	path:'/PostponeTransfer',
				// 	name:'PostponeTransfer',
				// 	component:()=>import('@/Views/Project/PostponeTransfer.vue')
				// }
			]
		},
		{
			//使用正则的方式，匹配任意的
			path: "/:path(.*)",
			component: () => import("../Views/404.vue")
		}
	]
})

//to,将要访问的路径
//from,代表从哪里来的,
//next 是一个函数,表示放行
//next() 直接放行,next('/login')强制跳转
router.beforeEach((to, from, next) => {
	//console.log(to);
	if (to.path.toLowerCase() === '/login') return next();
	if (to.path.toLowerCase() === '/help') return next();
	if (to.path.toLowerCase() === '/about') return next();
	//判断是否登录
	const token = window.sessionStorage.getItem('token')

	// // token不存在就强制跳转到登录
	if (!token) {
		return next('/login')
	}
	//防止重复登录
	// if (token && to.path.toLocaleLowerCase() == '/login') {
	//     return next('/home')
	// }
	// // 放行
	next();
})

export default router